<?php

class Functions {

    public static function array_remove_empty(& $arr) {
        foreach ($arr as $key => $value) {
            if (is_array($value)) {
                if (count($value)) {
                    self::array_remove_empty($arr[$key]);
                } else {
                    unset($arr[$key]);
                }
            } else {
                $value = trim($value);
                if ($value == '') {
                    unset($arr[$key]);
                }
            }
        }
    }

    public static function array_overlay($overlay) {
        $lastValue = array_pop($overlay);
        $arrayString = '$overlay_array';
        foreach ($overlay as $value) {
            $arrayString .= "['".$value."']";
        }
        $arrayString .= " = '';";
        eval($arrayString);
        $overlay_array = self::array_overlay_value($overlay_array, $lastValue);
        return $overlay_array;
    }

    private static function array_overlay_value($overlay_array, $lastValue) {
        foreach ($overlay_array as $key => $value) {
            if (is_array($value)) {
                $overlay_array[$key] = self::array_overlay_value($value, $lastValue);
            } else {
                $overlay_array[$key] = $lastValue;
            }
        }
        return $overlay_array;
    }

    public static function array_shrink($shrink, $parentName = '') {
        $shrink_array = array();
        foreach ($shrink as $key => $value) {
            if (is_array($value)) {
                if ($parentName) {
                    $temp = self::array_shrink($value, $parentName.'.'.$key);
                } else {
                    $temp = self::array_shrink($value, $key);
                }
                while (list($node, $nodevalue) = each($temp)) {
                    $shrink_array[$node] = $nodevalue;
                }
            } else {
                $shrink_array[$parentName.'.'.$key] = $value;
            }
        }
        return $shrink_array;
    }

    /* Reverse a Multibyte String in PHP */
    public static function mb_strrev($string, $encoding = null) {
        if ($encoding === null) {
            $encoding = mb_detect_encoding($string);
        }
        $length = mb_strlen($string, $encoding);
        $reversed = '';
        while ($length-- > 0) {
            $reversed .= mb_substr($string, $length, 1, $encoding);
        }
        return $reversed;
    }

}
